//
//  DataBase.m
//  豆瓣zlw
//
//  Created by lanou3g on 16/1/15.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "DataBase.h"


static DataBase *share=nil;

@implementation DataBase


+(DataBase *)shareData
{
    @synchronized(self)
    {
        if (share == nil)
        {
            share=[[DataBase alloc] init];
        }
        return share;
    }
}
//数据库对象
static sqlite3 *db=nil;
//*****************************打开数据库******************************
-(void)openData
{
    if (db == nil)
    {
        _filePath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"active.sqlite"];
        NSLog(@"%@",self.filePath);
        
        //打开数据库
        //.UTF8String把NSString转化成c的char
        int result=sqlite3_open(_filePath.UTF8String, &db);
        //创建那个数据库 才会在路径下面看到那个文件
        if (result == SQLITE_OK)
        {
            NSLog(@"打开成功");
            //创建一个表格
            NSString * sql = @"CREATE  TABLE  IF NOT EXISTS activeModel(active INTEGER PRIMARY KEY  NOT NULL , title TEXT,begin_time TEXT,end_time TEXT,address TEXT,participant_count TEXT,wisher_count TEXT,image TEXT ,name TEXT ,category_name TEXT ,content TEXT)";

            //exec就是用来执行sql语句的
            
         int result1=sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
            if (result1 == SQLITE_OK)
            {
                NSLog(@"创建表格成功");
            }else
            {
                NSLog(@"创建表格失败");
            }
        }
    }else
    {
        return;
    }
}
//*******************************************************************
//关闭数据库
-(void)closeData
{
    if (db != nil)
    {
        int result=sqlite3_close(db);
        if (result == SQLITE_OK)
        {
            NSLog(@"关闭数据库成功");
            db=nil;
        }else
        {
            NSLog(@"关闭数据库失败");
        }
    }
}

//*******************************************************************
//往数据库的表格里面添加数据
-(void)addActive:(ActiveModel *)act
{
    //把传进来的数据格式化放在sql与语句汇总

      NSString * sql = [NSString stringWithFormat:@"INSERT INTO activeModel(title ,begin_time , end_time ,address,participant_count,wisher_count,image,name,category_name,content) VALUES ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@')",act.title,act.begin_time,act.end_time,act.address,act.participant_count,act.wisher_count,act.image,act.name,act.category_name,act.content];
    //执行sql语句
    int result=sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK)
    {
        NSLog(@"添加数据成功");
    }else
    {
        NSLog(@"添加数据失败");
    }
}
//删除信息
-(void)deleteActive
{
        NSFileManager *manager=[NSFileManager defaultManager];
        [manager removeItemAtPath:_filePath error:nil];
}
//更新数据
-(void)updataActive:(ActiveModel *)act
{
    NSString *sql=[NSString stringWithFormat:@"UPDATE activeModel SET title = '%@'",act.title];
    int result=sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK)
    {
        NSLog(@"更新成功");
    }else
    {
        NSLog(@"更新失败");
    }
}
//查找数据
-(NSMutableArray *)selectAllActs
{
    NSMutableArray *array=[NSMutableArray array];
    NSString *aql=@"SELECT * FROM activeModel";
    sqlite3_stmt *stmt=nil;
    int result =sqlite3_prepare_v2(db, aql.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK)
    {
        NSLog(@"开始查找数据");
        //开始查找数据
        while (sqlite3_step(stmt)==SQLITE_ROW)
        {
            //拿到title的值
            char * title1=(char *)sqlite3_column_text(stmt, 1);
            NSString *title=[NSString stringWithCString:title1 encoding:NSUTF8StringEncoding];
            //拿到begin_time的值
            char *begin_time1=(char *)sqlite3_column_text(stmt, 2);
            NSString *begin_time=[NSString stringWithCString:begin_time1 encoding:NSUTF8StringEncoding];
            //拿到end_time的值
            char *end_time1=(char *)sqlite3_column_text(stmt, 3);
            NSString *end_time=[NSString stringWithCString:end_time1 encoding:NSUTF8StringEncoding];
            //拿到address的值
            char *address1=(char *)sqlite3_column_text(stmt, 4);
            NSString *address=[NSString stringWithCString:address1 encoding:NSUTF8StringEncoding];
            //拿到participant_count的值
            char *participant_count1=(char *)sqlite3_column_text(stmt, 5);
            NSString *participant_count=[NSString stringWithCString:participant_count1 encoding:NSUTF8StringEncoding];
            //拿到wisher_count的值
            char *wisher_count1=(char *)sqlite3_column_text(stmt, 6);
            NSString *wisher_count=[NSString stringWithCString:wisher_count1 encoding:NSUTF8StringEncoding];
            
            //拿到image的值
            char *image1=(char *)sqlite3_column_text(stmt, 7);
            NSString *image=[NSString stringWithCString:image1 encoding:NSUTF8StringEncoding];
            //拿到name的值
            char *name1=(char *)sqlite3_column_text(stmt, 8);
            NSString *name=[NSString stringWithCString:name1 encoding:NSUTF8StringEncoding];
            //拿到category_name的值
            char *categpry1=(char *)sqlite3_column_text(stmt, 9);
            NSString *category=[NSString stringWithCString:categpry1 encoding:NSUTF8StringEncoding];
            //拿到content的值
            char *content1=(char *)sqlite3_column_text(stmt, 10);
            NSString *content=[NSString stringWithCString:content1 encoding:NSUTF8StringEncoding];
            ActiveModel *activeModel=[[ActiveModel alloc] init];
            activeModel.title=title;
            activeModel.begin_time=begin_time;
            activeModel.end_time=end_time;
            activeModel.address=address;
            activeModel.participant_count=[NSNumber numberWithInt:[participant_count intValue]];
            activeModel.wisher_count=[NSNumber numberWithInt:[wisher_count intValue]];
            activeModel.image=image;
            activeModel.name=name;
            activeModel.category_name=category;
            activeModel.content=content;
            
            [array addObject:activeModel];
            
        }
    }else
    {
        NSLog(@"查找失败");
    }
    //释放游标
    sqlite3_finalize(stmt);
 
    return array;

}



@end
